#include "protocol.hpp"

#include <iostream>
#include <string>
#include <memory>
#include <sstream>
#include <json/json.h>

void test();

int main()
{
	Json::Value root;
	root["name"] = "rich";
	root["six"] = "男";
	root["age"] = 25;

	// //方案1
	// std::string s = root.toStyledString();
	// std::cout << s << std::endl;
	// std::cout << "***************" << std::endl;

	// //方案2
	// Json::StreamWriterBuilder wbuilder;
	// std::unique_ptr<Json::StreamWriter> writer(wbuilder.newStreamWriter());
	// std::stringstream ss;
	// writer->write(root, &ss);
	// std::cout << ss.str() << std::endl;
	// std::cout << "****************" << std::endl;

	// 方案3--不安全
	Json::FastWriter writer2;
	// Json::StyledWriter writer2;
	std::string s2 = writer2.write(root);
	std::cout << s2 << std::endl;
	std::cout << "****************" << std::endl;

	Json::Value root1;
	Json::Reader read1;

	bool ret_rea = read1.parse(s2, root1);
	if (!ret_rea)
	{
		std::cout << "reader parse error" << std::endl;
		std::cout << "****************" << std::endl;
	}
	else
	{
		std::string str1 = root1["name"].asString();
		std::string str2 = root1["six"].asString();
		int in3 = root1["age"].asInt();
		std::cout << "reader parse success" << std::endl;
		std::cout << "name = " << str1
				  << "  six = " << str2
				  << "  age = " << in3
				  << std::endl;
		std::cout << "****************" << std::endl;
	}

	return 0;
}

void test()
{
	int ma_x = 0;
	int ma_y = 0;
	char ma_oper = '+';
	while (1)
	{
		std::cin >> ma_x >> ma_y >> ma_oper;
		// 建立请求对象
		Request ma_req(ma_x, ma_y, ma_oper);
		// 序列化
		std::string ma_reqser = ma_req.Serialize();
		std::cout << ma_reqser << std::endl;
		// 建立应答对象
		Response ma_res;
		// 反序列化
		ma_res.Deserialize(ma_reqser);
		// 序列化
		std::string ma_resser = ma_res.Serialize();
		std::cout << ma_resser << std::endl;
		// 反序列化
		int lo_cord = ma_req.Deserialize(ma_resser);
		// 获得结果
		if (lo_cord == 0)
		{
			int lo_value = ma_req.Get();
			std::cout << "success " << lo_value << std::endl;
		}
		else
		{
			std::cout << "error" << std::endl;
		}
	}
}